package cn.fintecher.gis.service;

import cn.fintecher.gis.entity.Operator;
import cn.fintecher.gis.entity.Resource;
import cn.fintecher.gis.entity.RoleInfo;
import cn.fintecher.gis.enums.CommonState;
import cn.fintecher.gis.exception.BadRequestException;
import cn.fintecher.gis.model.session.SessionModel;
import cn.fintecher.gis.model.response.OperatorResponse;
import cn.fintecher.gis.model.response.ResourceResponse;
import cn.fintecher.gis.respository.OperatorRepository;
import cn.fintecher.gis.utils.BeanUtils;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by ChenChang on 2018/9/3.
 */
@Service("operatorService")
public class OperatorService {
    @Autowired
    private OperatorRepository operatorRepository;
    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private RedisTemplate<String, SessionModel> sessionOperatorRedisTemplate;


    public void operatorLogin(Operator operator, String token) {
        SessionModel sessionModel = modelMapper.map(operator, SessionModel.class);

        sessionOperatorRedisTemplate.opsForValue().set("operator:token:" + token, sessionModel);
    }

    public void operatorOut(String token) {
        sessionOperatorRedisTemplate.delete("operator:token:" + token);
    }

    public SessionModel findOperatorByToken(String token) {
        SessionModel operator = sessionOperatorRedisTemplate.opsForValue().get("operator:token:" + token);
        return operator;
    }

    /**
     * 将数据实体转化业务实体
     *
     * @param operator
     * @return
     */
    public OperatorResponse mergeOperator(Operator operator) {
        OperatorResponse operatorResponse = new OperatorResponse();
        BeanUtils.copyPropertiesIgnoreNull(operator,operatorResponse);
        Set<ResourceResponse> resourceResponses = new HashSet<>();
        Set<RoleInfo> roles = operator.getRoles();
        for (RoleInfo roleInfo : roles) {
            if(roleInfo.getRoleStatus().equals(CommonState.ENABLED)) {
                Set<Resource> resources = roleInfo.getResources();
                for (Resource resource : resources) {
                    ResourceResponse resourceResponse = new ResourceResponse();
                    BeanUtils.copyPropertiesIgnoreNull(resource, resourceResponse);
                    if (Objects.nonNull(resource.getParent())) {
                        resourceResponse.setParentId(resource.getParent().getId());
                    }
                    resourceResponses.add(resourceResponse);
                }
            }
        }
        List<ResourceResponse> resourceResponseList = new ArrayList<>(resourceResponses);
        operatorResponse.setResourceResponses(resourceResponseList);
        return operatorResponse;
    }


    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    public Operator getOperatorById(String id){
        Optional<Operator> optional = operatorRepository.findById(id);
        if(!optional.isPresent()){
            throw new BadRequestException("","the.user.for.the.query.does.not.exist");
        }
        Operator operator = optional.get();
        return operator;
    }

}
